package com.aote.util;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.aote.entity.entityfinder.EntityFinder;
import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.Tools;
import com.browsesoft.baseadapter.BasicEntity;
import com.browsesoft.baseadapter.SimpleEntity;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLPage;

public class EntityHelper {
	/**
	 * 根据请求和组件元素创建实体
	 * 
	 * @param request
	 * @param element
	 */
	public static Entity createEntities(HttpServletRequest request,
			Element element, HTMLBasicComponent component) {
		try {
			// 从组件中得到查找实体的类
			String ent = element.getAttribute("entityfind");
			if (ent == null || ent == "") {
				return null;
			}
			EntityFinder enfind = (EntityFinder) Class.forName(ent)
					.newInstance();
			Entity entity = enfind.getEntity(request, element, component);
			return entity;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 从实体列表中得到对象属性，把所有属性组织成一个串，属性之间用","分割。
	 * 
	 * @param iter
	 * @return
	 */
	public static String entitiesToIDString(Iterator iter, String attrName) {
		String result = "";
		while (iter.hasNext()) {
			Entity entity = (Entity) iter.next();
			result += entity.getAttributes().get(attrName) + ",";
		}
		if (result.endsWith(",")) {
			result = result.substring(0, result.length() - 1);
		}
		return result;
	}

	/**
	 * 根据给定一批实体id号，得到实体列表
	 */
	public static List getEntities(String entityType, String ids) {
		List result = new LinkedList();
		// 对于每一个id号
		Iterator iter = Tools.stringToLinkedList(ids).iterator();
		while (iter.hasNext()) {
			String id = (String) iter.next();
			// 找到对象，放到结果中
			HTMLPage page = (HTMLPage) ThreadLocals.page.get();
			result.addAll(EntityManager.getInstance().getEntities(entityType,
					"id", id, page));
		}
		return result;
	}

	/**
	 * 复制一批实体
	 */
	public static List cloneEntities(List entities) {
		List result = new LinkedList();
		Iterator iter = entities.iterator();
		while (iter.hasNext()) {
			SimpleEntity entity = (SimpleEntity) iter.next();
			Entity nEntity = (Entity) entity.clone();
			result.add(nEntity);
		}
		return result;
	}

	/**
	 * 删除一批实体
	 */
	public static void deleteEntities(List entities) {
		// 没有，不删除
		if(entities == null) {
			return;
		}
		try {
			Iterator iter = entities.iterator();
			while (iter.hasNext()) {
				SimpleEntity entity = (SimpleEntity) iter.next();
				entity.delete();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 把一批实体转换成 entitytyp:id{;entitytyp:id}形式字符串
	 */
	public static String entitiesToMaps(List entities) {
		LinkedList list = new LinkedList();
		Iterator iter = entities.iterator();
		while(iter.hasNext()) {
			BasicEntity entity = (BasicEntity)iter.next();
			String etype = entity.getEntityType();
			String id = entity.getID();
			String value = id + ":" + etype;
			list.add(value);
		}
		String result = Tools.LinkedListTostring(list, ";");
		return result;
	}
}
